package com.server.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.IFinanceService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("finance")
public class FinanceController {

	@Autowired
	private IFinanceService financeService;
	
	@RequestMapping("")
	public ModelAndView orgPage(){
		ModelAndView mv = new ModelAndView("rewardCenter/finance/finance");
		mv.addObject("projectName",ProjectInfor.PLATFORM_NAME);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	
	@RequestMapping("list/{type}")
	public void list(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type){
		/*type:0待审核，1已审核，2已返费*/
		String cardId = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		DataVector<Map<String, Object>> listData = financeService.getList(new RequestInput(request),type); 
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(financeService.listAddSourceName(listData.getList()), null, null));
	}
	
	@RequestMapping("check")
	public ModelAndView check(){
		ModelAndView mv = new ModelAndView("rewardCenter/check/checkList");
		mv.addObject("projectName",ProjectInfor.PLATFORM_NAME);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("check/{state}")
	public void check(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer state){
		String cardId = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		String ids = request.getParameter("ids");
		Boolean isSuccess = financeService.check(cardId, ids,state);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	@RequestMapping("remit")
	public void remit(HttpServletRequest request,
			HttpServletResponse response){
		String cardId = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		String ids = request.getParameter("ids");
		Boolean isSuccess = financeService.remit(cardId, ids);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	
	@RequestMapping("detail/{id}")
	public ModelAndView detail(HttpServletRequest request,@PathVariable Integer id){
		ModelAndView mv = new ModelAndView("rewardCenter/finance/detail");
		mv.addObject("projectName",ProjectInfor.PLATFORM_NAME);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("id",id);
		return mv;
	}
	@RequestMapping("detailList/{id}")
	public void detailList(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id){
		String cardId = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		DataVector<Map<String, Object>> listData = financeService.getDetailList(new RequestInput(request), id);
		OutputUtil.returnString(response, JSONSerielizer.toJson(financeService.rewardDetailAddSourceName(listData.getList()), null, null));
	}
	
}
